using System;
using Microsoft.SPOT;

namespace Bt.WebServer
{
    public class Router
    {
        public Router()
        {
            this.Controller = "Home";
            this.Action = "Index";
        }
        public Router(string url)
            : this()
        {
            //GET /Door/DoorState

            int idx = 0;
            char ch;
            for (int i = 0; i < url.Length; i++ )
            {
                ch = url[i];
                if (idx == 0)
                {
                    if (ch == '/')
                    {
                        idx++;
                        this.Controller = "";
                        continue;
                    }
                }
                else if (idx == 1)
                {
                    if (!this.CharIsAlphaBetic(ch))
                    {
                        idx++;
                        this.Action = "";
                        continue;
                    }
                    this.Controller += ch;
                }
                else if (idx == 2)
                {
                    if (!this.CharIsAlphaBetic(ch))
                    {
                        idx++;
                        continue;
                    }
                    this.Action += ch;
                }
            }
        }

        private bool CharIsAlphaBetic(char ch)
        {
            return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z';
        }

        public string Controller { get; set; }
        public string Action { get; set; }
        public string[] Parameters { get; set; }
        public string[] Keys { get; set; }
    }
}